# Create your views here.
from django.contrib import auth
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_protect
from django.core.context_processors import csrf
from django.template import RequestContext
from django.contrib.auth.models import User
import urllib2, urllib
from django.shortcuts import *
from mm.dashboard.models import *

@csrf_protect
def login(request):
    user = request.user
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = auth.authenticate(username=username, password=password)
        if user:
            if user.is_active:
                auth.login(request,user)
                return redirect('/')
            else:
                return render_to_response('dashboard/disabled_account.html')
        else:
            return render_to_response('dashboard/invalid_account.html')
    return render_to_response('dashboard/login.html', {'user': request.user}, context_instance=RequestContext(request))

def logout_view(request):
    logout(request)
    return redirect('/')

@login_required 
def dashboard(request):
    
    machines = Machine.objects.all()
    count = 0 
    for machine in machines:
        for s in machine.services.keys(): 
            count += 1
    nmachines = len(machines)
    user = request.user             
    return render_to_response("dashboard/dashboard.html", { 'user': user, 'machines': machines, 'nmachines': nmachines, 'nservices': count } )

@login_required            
def host_detail(request):
    try:
        machine = request.GET['machine']
    except KeyError:
        return render_to_response('dashboard/host_detail.html', {'machine': None})
    
    machine_obj = Machine.objects.get(id=machine)
    return render_to_response('dashboard/host_detail.html', {'machine': machine_obj})

@login_required        
def postrequest(request):
    try:
        machine = request.GET['machine']
        service = request.GET['service']
        action = request.GET['action']
    except KeyError:
        pass
  
    machine_obj = Machine.objects.get(id=machine) 
    url = 'http://%s/%s' % (machine_obj.hostname, service)
    values = { 'action': action }
    data = urllib.urlencode(values)
    url = urllib2.urlopen(url, data)
    rc = url.getcode()
    if rc == 200:
        success = True
    else:
        success = False
    return redirect('/services/detail/?machine=%s&service=%s&success=%s&action=%s' %(machine_obj.id, service, success, action))

    #return render_to_response('dashboard/service_detail.html', {'machine': machine_obj, 'service': service, 'success': success, 'action': action})
@login_required
def service_detail(request):
    if request.method == "GET":
        try:
            service = request.GET['service']
            machine = request.GET['machine']
        except KeyError:
            return render_to_response('dashboard/service_detail.html', {'machine': None} )

        try:
            success = request.GET['success']
            action = request.GET['action']
        except KeyError:
            success = None
            action = None
            pass

        machine_obj = Machine.objects.get(id=machine)
        return render_to_response('dashboard/service_detail.html', {'machine': machine_obj, 'service': service, 'success': success, 'action': action})
    
        
